﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SuperSprintZ
{
    class Highscore
    {
        public int rataid;
        private int minuutit;
        private int sekunnit;
        private int sadasosat;
        private String nimi;
        private static Highscore[][] topscores;
        private static StreamWriter writer;
        private static FileStream targetfile;

        public Highscore(int rata, int minutes, int seconds, int hundreth_of_a_second, String nick)
        {
            rataid = rata;
            minuutit = minutes;
            sekunnit = seconds;
            sadasosat = hundreth_of_a_second;
            nimi = nick;
        }

        public Highscore(int rata)
        {
            rataid = rata;
            minuutit = 99;
            sekunnit = 59;
            sadasosat = 99;
            nimi = "NoName";
        }

        public String highscoreToString()
        {
            return minuutit + " " + sekunnit + " " + sadasosat + " " + nimi;
        }

        public static bool addResult(int trackid, int min, int sec, int and_parts, String nikki)
        {
            Highscore uusi = new Highscore(trackid, min, sec, and_parts, nikki);
            int siirtokohta = -1;
            if(uusi.compareTo(topscores[trackid][9]) > 0){
                for (int i = 0; i < 10; i++)
                {
                    if(uusi.compareTo(topscores[trackid][i]) > 0){
                        siirtokohta = i;
                        break;
                    }
                }
                for (int i = 9, j = 8; j >= siirtokohta; i--, j--)
                {
                    topscores[trackid][i] = topscores[trackid][j];
                }
                topscores[trackid][siirtokohta] = uusi;

                saveList(trackid);
                return true;
            }
            return false;
        }

        public int compareTo(Highscore another)
        {
            if (this.minuutit < another.minuutit){
                return 1;
            }
            if(this.minuutit == another.minuutit && this.sekunnit < another.sekunnit){
                return 1;
            }
            if (this.minuutit == another.minuutit && this.sekunnit == another.sekunnit && this.sadasosat < another.sadasosat)
            {
                return 1;
            }
            if (this.minuutit == another.minuutit && this.sekunnit == another.sekunnit && this.sadasosat == another.sadasosat)
            {
                return 0;
            }
            return -1;
        }

        public static void setUpHighscoreList(int[] track_ids){
            if (!Directory.Exists("highscores"))
            {
                Directory.CreateDirectory("highscores");
            }
            if (!File.Exists("highscores/tracks.txt"))
            {
                targetfile = File.Create("highscores/tracks.txt");
                writer = new StreamWriter(targetfile);
                for (int k = 0; k < track_ids.Length; k++)
                {
                    writer.WriteLine(track_ids[k]);
                }
                writer.Close();
            }
            for(int i = 0; i< track_ids.Length; i++){
                if (!File.Exists("highscores/" + track_ids[i] + ".txt")) //if the file for a track doesn't exitst yet, make one
                {
                    targetfile = File.Create("highscores/" + track_ids[i] + ".txt");
                    writer = new StreamWriter(targetfile);
                    for (int j = 0; j < 10; j++)
                    {
                        writer.WriteLine(new Highscore(i).highscoreToString());
                    }
                    writer.Close();
                    //writes tens lines of crap to that file
                }
            }
        }

        public static String[] readHighscores(String rataid)
        {
            return File.ReadAllLines("highscores/" + rataid +".txt");
        }


        public static Highscore[][] loadLists()
        {
            String[] tracks = readHighscores("tracks");
            topscores = new Highscore[tracks.Length][];
            String[] lines;
            String[] line;
            int min;
            int sec;
            int tensofmillis;
            String name;
            for (int i = 0; i < tracks.Length; i++)
            {
                lines = readHighscores(tracks[i]);
                topscores[i] = new Highscore[10];
                for (int j = 0; j < 10; j++)
                {
                    line = lines[j].Split(' ');
                    min = Int32.Parse(line[0]);
                    sec = Int32.Parse(line[1]);
                    tensofmillis = Int32.Parse(line[2]);
                    name = line[3];
                    topscores[i][j] = new Highscore(i, min, sec, tensofmillis, name);
                }
            }
            
            return topscores;
        }

        public static void saveList(int trackid)
        {
            targetfile = File.Create("highscores/" + trackid + ".txt");
            writer = new StreamWriter(targetfile);
            for (int j = 0; j < 10; j++)
            {
                writer.WriteLine(topscores[trackid][j].highscoreToString());
            }
            writer.Close();
        }
    }
}
